package com.zenitera.bigdata.runningmode;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * 当处理的有界数据应该使用BATCH执行模式, 这样更高效. 当处理的是无界数据, 则必须使用STREAMING 执行模式, 因为这种模式才能处理持续的数据流
 * STREAMING模式下, 数据是来一条输出一次结果
 * BATCH模式下, 数据处理完之后, 一次性输出结果
 * 如果是有界的source: 可以使用流模式也可以使用批模式
 * 如果是无界的source: 只能使用流模式
 */
public class Flink01_RuntimeMode_2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(1);
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);

        env
                .socketTextStream("wangtingflink03", 6666)
                .flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
                    @Override
                    public void flatMap(String s, Collector<Tuple2<String, Long>> out) throws Exception {
                        for (String word : s.split(" ")) {
                            out.collect(Tuple2.of(word, 1L));
                        }

                    }
                })
                .keyBy(t -> t.f0)
                .sum(1)
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

/*
[wangting@wangtingflink03 ~]$ nc -lk 6666
wang wang wang
ting ting aaa zero

-------------------
(wang,1)
(wang,2)
(wang,3)
(ting,1)
(ting,2)
(aaa,1)
(zero,1)

Process finished with exit code -1
 */
